<template>
  <transition name="slide-fade">
    <div class="wrapper" v-if="keyBoardShow">
      <div class="left left-container">
        <span class="num" v-for="(item,index) in numBoards" :key="index" @click="keyboardClick(item)">{{item}}</span>
        <span class="num keyBoardIcon" @click="keyBoardShow = false"></span>
      </div>
      <div class="right right-container">
        <div class="delete" @click="deleteWords"></div>
        <div class="ok white_color" @click="keyBoardShow = false">确认</div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  data: () => ({
    keyBoardShow: false, // 控制键盘的弹起和收回
    numBoards: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.' , '0'],
    num: ''
  }),
  props: {
    'sp-key': {
      type: String,
      default: ''
    },
    max: {
      type: Number,
      default: 10
    }
  },
  methods: {
    handleInit() {  // 数字键盘组件内部的初始化方法，用于重新渲染组件
      if (this.spKey) {
        this.numBoards = this.numBoards.join('').replace(/\./, 'X').split('')
      }
    },
    handleClear() { //清除之前输入的字符组合，并触发
    },
    keyboardToggle(val) { // 控制键盘的弹起和收回事件
      this.keyBoardShow = val
    },
    deleteWords() {
      let arr = String(this.num).split('')
      arr.splice(arr.length-1, 1)
      this.num = arr.join('')
      this.$emit('keyboardClick', this.num)
    },
    keyboardClick(words) {
      if (this.num.length < this.max) {
        if (this.num.indexOf('.') === -1) {
          if (this.num.length === 0 && (/\./).test(words)){
            this.num = '0' + String(words)
          } else {
            this.num += String(words)
          }
          this.$emit('keyboardClick', this.num)
        } else {
          if ((/\d/).test(words)) {
            let arr = this.num.split('.')
            if (arr[1].length < 2){
              this.num += String(words)
              this.$emit('keyboardClick', this.num)
            }
          }
        }
      }
    }
  }
}
</script>

<style lang="css" scoped>
div {
  box-sizing: border-box;
}
.slide-fade-enter-active {
  transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-leave-active {
  transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to {
  transform: translateY(10px);
  opacity: 0;
}
.wrapper {
  width: 100%;
  border-top: 1px solid #e2e2e2;
  position: fixed;
  bottom: 0;
  left:0;
  background-color: #fff;
}
.left-container {
  width: 75%;
}
.num {
  display: block;
  float: left;
  height: 3rem;
  line-height: 3rem;
  width: 33.33%;
  text-align: center;
  border-bottom: 1px solid #e2e2e2;
  border-right: 1px solid #e2e2e2;
}
.num:nth-child(10),.num:nth-child(11),.num:nth-child(12) {
  border-bottom: none;
}
.keyBoardIcon {
  background-image: url('../../../images/keyBoard.png');
  background-repeat: no-repeat;
  background-size: 30%;
  background-position: center;
}
.right-container {
  width: 25%;
}
.right-container {
  text-align: center;
  line-height: 6rem;
}
.delete {
  width: 100%;
  height: 6rem;
  background-repeat: no-repeat;
  background-size: 50%;
  background-position: center;
  background-image: url('../../../images/keyBoard-del.png');
}
.ok {
  background: #2e4f8f;
}
</style>
